package com.niiwoo.civet.trade.service.local.loan;

import com.niiwoo.civet.trade.dao.entity.DiversionConfig;
import com.niiwoo.civet.trade.dao.entity.LoanBusinessInfo;
import com.niiwoo.civet.trade.dao.entity.SmallLoanWhiteUsers;
import com.niiwoo.civet.trade.dao.mapper.DiversionConfigMapperExt;
import com.niiwoo.civet.trade.dao.mapper.LoanBusinessInfoMapperExt;
import com.niiwoo.civet.trade.dao.mapper.SmallLoanWhiteUsersMapperExt;
import com.niiwoo.civet.trade.dto.common.LoanBusinessInfoDTO;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * Created by dell on 2018/4/18.
 * Description：civet-app
 */
@Slf4j
@Service
public class LoanBusinessLocalService {

    @Autowired
    private LoanBusinessInfoMapperExt loanBusinessInfoMapperExt;

    @Autowired
    private SmallLoanWhiteUsersMapperExt smallLoanWhiteUsersMapperExt;

    @Autowired
    private DiversionConfigMapperExt diversionConfigMapperExt;

    /**
     * 查询小易借白名单
     * @param userId
     * @return
     */
    public SmallLoanWhiteUsers querySmallLoanWhite(String userId){
        return smallLoanWhiteUsersMapperExt.selectByPrimaryKey(userId);
    }

    /**
     * 获取借款产品列表
     * @return
     */
    public List<LoanBusinessInfoDTO> getProductList() {
        List<LoanBusinessInfoDTO> productList = new ArrayList<>();
        List<LoanBusinessInfo> loanBusinessInfos = loanBusinessInfoMapperExt.queryProductListEnable();
        if (!loanBusinessInfos.isEmpty()){
            for (LoanBusinessInfo loanBusinessInfo : loanBusinessInfos) {
                LoanBusinessInfoDTO loanBusinessInfoDTO = new LoanBusinessInfoDTO();
                BeanUtils.copyProperties(loanBusinessInfo, loanBusinessInfoDTO);
                productList.add(loanBusinessInfoDTO);
            }
        }

        // 导流产品
        List<DiversionConfig> diversionConfigList = diversionConfigMapperExt.selectEffectiveData();
        diversionConfigList.forEach(cfg -> {
            LoanBusinessInfoDTO loanBusinessInfoDTO = new LoanBusinessInfoDTO();

            loanBusinessInfoDTO.setProductType((byte) 99);
            loanBusinessInfoDTO.setProductName(cfg.getProductName());
            loanBusinessInfoDTO.setProductDesc(cfg.getProductDesc());
            loanBusinessInfoDTO.setMinAmount(cfg.getMinAmount());
            loanBusinessInfoDTO.setMaxAmount(cfg.getMaxAmount());
            loanBusinessInfoDTO.setIconUrl(cfg.getLogoUrl());
            loanBusinessInfoDTO.setJumpUrl(cfg.getJumpUrl());
            loanBusinessInfoDTO.setSeq(cfg.getSeq());
            loanBusinessInfoDTO.setCreateTime(cfg.getCreateTime());

            productList.add(loanBusinessInfoDTO);
        });

        // 先按排序值从小到大排序，再按创建时间先后排序
        productList.sort(new Comparator<LoanBusinessInfoDTO>() {
            @Override
            public int compare(LoanBusinessInfoDTO o1, LoanBusinessInfoDTO o2) {
                if (o1.getSeq() != null && o2.getSeq() != null) {
                    if (o1.getSeq() > o2.getSeq()) {
                        return 1;
                    } else if (o1.getSeq() < o2.getSeq()) {
                        return -1;
                    } else {
                        if (o1.getCreateTime() != null && o2.getCreateTime() != null) {
                            if (o1.getCreateTime().after(o2.getCreateTime())) {
                                return 1;
                            } else {
                                return -1;
                            }
                        } else if (o1.getCreateTime() != null && o2.getCreateTime() == null) {
                            return -1;
                        } else if (o1.getCreateTime() == null && o2.getCreateTime() != null) {
                            return 1;
                        }
                    }
                } else if (o1.getSeq() != null && o2.getSeq() == null) {
                    return -1;
                } else if (o1.getSeq() == null && o2.getSeq() != null) {
                    return 1;
                } else {
                    if (o1.getCreateTime() != null && o2.getCreateTime() != null) {
                        if (o1.getCreateTime().after(o2.getCreateTime())) {
                            return 1;
                        } else {
                            return -1;
                        }
                    } else if (o1.getCreateTime() != null && o2.getCreateTime() == null) {
                        return -1;
                    } else if (o1.getCreateTime() == null && o2.getCreateTime() != null) {
                        return 1;
                    }
                }
                return 0;
            }
        });

        return productList;
    }

    /**
     * 获取产品配置
     */
    public LoanBusinessInfoDTO selectByProductType(Byte productType){
        LoanBusinessInfo loanBusinessInfo = loanBusinessInfoMapperExt.selectByProductType(productType);
        LoanBusinessInfoDTO loanBusinessInfoDTO = null;
        if(loanBusinessInfo != null){
            loanBusinessInfoDTO = new LoanBusinessInfoDTO();
            BeanUtils.copyProperties(loanBusinessInfo, loanBusinessInfoDTO);
        }
        return loanBusinessInfoDTO;
    }
}
